﻿
// actionFlag: 1 -> 新增；2 -> 修改；3 -> 查看
var actionFlag = getPar1("actionFlag");

$(document).ready(function () {
    $("#MinOrderQty").numberbox({
        min: 0,
        precision: 0
    });

    $("#MaxOrderQty").numberbox({
        min: 0,
        precision: 0
    });

    $("#Leadtime").numberbox({
        min: 0,
        precision: 0
    });

    $("#KeepTime").numberbox({
        min: 0,
        precision: 0
    });

    if (actionFlag == 2) {
        $("#ItemNo").attr("disabled", "disabled");
        parent.setAddNewButtonHiden();
    }

    $("#ItemNo").bind("change", function () { var itemNo = $(this).val(); isExistsItemNo(itemNo); });
    $("#ItemModel").bind("change", function () { var itemModel = $(this).val(); isExistsItemModel(itemModel); });
    $("#ItemBarcode").bind("change", function () { var itemBarcode = $(this).val(); isExistsItemBarcode(itemBarcode); });

    
});

// 检测物料编号是否重复
function isExistsItemNo(val) {
    if (val != "") {
        var itemNo = fnGetData("BH_ItemMaster", "ItemID<>'" + instance[0].ItemID.toString() + "' and ItemNo='" + val + "'");
        if (itemNo != null && itemNo != "") {
            alert("物料编号: [ " + val + " ] 已存在, 请重新输入!");
            setTimeout(function () {
                $("#ItemNo").focus();
                $("#ItemNo").val("");
            }, 10);
        }
    }
}

// 检测规格名称是否重复
function isExistsItemModel(val) {
    if (val != "") {
        var itemModel = fnGetData("BH_ItemMaster", "ItemID<>'" + instance[0].ItemID.toString() + "' and ItemModel='" + val + "'");
        if (itemModel != null && itemModel != "") {
            alert("规格名称: [ " + val + " ] 已存在, 请重新输入!");
            setTimeout(function () {
                $("#ItemModel").focus();
                $("#ItemModel").val("");
            }, 10);
        }
    }
}

// 检测条码是否重复
function isExistsItemBarcode(val) {
    if (val != "") {
        var itemBarcode = fnGetData("BH_ItemMaster", "ItemID<>'" + instance[0].ItemID.toString() + "' and ItemBarcode='" + val + "'");
        if (itemBarcode != null && itemBarcode != "") {
            alert("物料条码: [ " + val + " ] 已存在, 请重新输入!");
            setTimeout(function () {
                $("#ItemBarcode").focus();
                $("#ItemBarcode").val("");
            }, 10);
        }
    }
}

// 提交检测
var fnCustomCheck = function () {
    if (actionFlag == 1) {
        if (instance[0].ItemNo == "" || instance[0].ItemNo == null) {
            $("#ItemNo").focus();
            return "物料编号不能为空!";
        }
    }
    if (instance[0].ItemName == "" || instance[0].ItemName == null) {
        $("#ItemName").focus();
        return "物料名称不能为空!";
    }
    if (instance[0].ItemModel == "" || instance[0].ItemModel == null) {
        $("#ItemModel").focus();
        return "规格名称不能为空!";
    }
    if (instance[0].ItemType == "" || instance[0].ItemType == null) {
        $("#ItemType").focus();
        return "物料类型不能为空!";
    }
    if (instance[0].ItemType == "M" || instance[0].ItemType == "G") {
        if (instance[0].WorkCenter == "" || instance[0].WorkCenter == null) {
            $("#WorkCenter").focus();
            return "工作中心不能为空!";
        }
    }
    return "";
}

var fnSetValue = function () {
    instance[0].ItemName = instance[0].ItemName.toUpperCase();
    instance[0].ItemModel = instance[0].ItemModel.toUpperCase();
    instance[0].EnglishName = instance[0].EnglishName.toUpperCase();
    instance[0].ItemDes = instance[0].ItemDes.toUpperCase();
    instance[0].MinOrderQty = $("#MinOrderQty").val() == "" ? "0" : $("#MinOrderQty").val();
    instance[0].MaxOrderQty = $("#MaxOrderQty").val() == "" ? "0" : $("#MaxOrderQty").val();
    instance[0].Leadtime = $("#Leadtime").val() == "" ? "0" : $("#Leadtime").val();
    instance[0].StandPackQty = "1";
    instance[0].ExchangeRate = "1";
    instance[0].StandPrice = "0.0000"
    instance[0].KeepTime = $("#KeepTime").val() == "" ? "0" : $("#KeepTime").val();
}


var fnCustomSave = function () {
    fnSetValue();

    json = $.toJSON(instance[0]).replace(/'/g, "[B]").replace(/\+/g, "[J]");
    dataAction = (actionFlag == 1) ? "A" : "C";

    $.ajax({
        async: false, //异步请求
        cache: false, //不从浏览器缓存中加载
        type: "GET",
        /*dataType: "json",*/
        url: "/DCBH/DCBHCommonOperator.aspx",
        data: { Action: "procedure", entity: "BH_ItemMaster", json: json, dataAction: dataAction },
        success: function (data) {
            //debugger;
            if (data != "") {
                if (data == "1") {
                    alert("数据保存成功!");

                    if (parent.SaveAndAddNewFlag == true) {
                        location.href = location.href
                    }

                    if (parent.SaveSuccessClose == true) {
                        parent.parent.CloseCurPage();
                    }
                }
                else {
                    alert(data);
                }
            }
        },
        error: function (e) {
            alert("保存失败, 请联系开发人员，核查系统！");
            return;
        }
    });
}